package com.guochuang.yunwang.promanager.controller;

import com.github.pagehelper.PageInfo;
import com.guochuang.yunwang.promanager.annotation.CancelLoginIntercept;
import com.guochuang.yunwang.promanager.domain.Role;
//import com.guochuang.yunwang.promanager.domain.Role;
//import com.guochuang.yunwang.promanager.domain.vo.RoleVo;
import com.guochuang.yunwang.promanager.domain.vo.RoleVO;
import com.guochuang.yunwang.promanager.service.RoleService;
import com.guochuang.yunwang.promanager.utils.ProjectConstant;
import com.guochuang.yunwang.promanager.utils.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//import javax.management.relation.Role;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.util.List;

@RestController
@Api(value = "角色接口",tags = "角色接口")
@Slf4j
public class RoleController {
    @Autowired
    private SimpleDateFormat simpleDateFormat;
    @Autowired
    private RoleService roleService;

    @GetMapping(value = "/get/all/role/list")
    @ApiOperation(value = "获取所有的角色list")
    public ResultEntity getAllRolesList(){
        List<Role> allRoles = roleService.getAllRoles();

        return ResultEntity.successWithData(allRoles);
    }


    @GetMapping(value = "/get/role/auth/by/{rid}")
    @ApiOperation(value = "根据角色id获取角色的权限id")
    @CancelLoginIntercept
    public ResultEntity getRoleAuthByRid(@PathVariable Integer rid){
        log.info(String.format("获取角色id为{%d}的权限", rid));

        List<Integer> rec = roleService.getRoleAuthByRid(rid);

        return ResultEntity.successWithData(rec);
    }

    /**
     * 分页获取角色列表
     * @return
     */
    @GetMapping(value = "/get/role/info/{page}/{offset}")
    @ApiOperation(value = "分页获取角色的信息")
    @CancelLoginIntercept
    public ResultEntity getRoleList(@PathVariable Integer page,
                                    @PathVariable Integer offset){

        PageInfo pageInfo = roleService.getRoleByPage(page,offset);

        Map<String,Object> rec = new HashMap<>();

        rec.put("total",pageInfo.getTotal());

        rec.put("data",pageInfo.getList());

        return ResultEntity.successWithData(rec);
    }

    @PutMapping(value = "/update/role/auths/{rid}")
    @ApiOperation(value = "修改角色权限")
    @CancelLoginIntercept
    public ResultEntity updateAuthToRole(@PathVariable Integer rid,@RequestBody List<Integer> aids){

        Integer nums = roleService.updateRoleAuths(rid,aids);

        if(nums>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed(ProjectConstant.USER_CHANGE_FAILED);
        }
    }

    @PostMapping(value = "/add/new/roleandauths/{aids}")
    @ApiOperation(value = "添加新的角色并初始化角色权限")
    @CancelLoginIntercept
    public ResultEntity addNewRoleAuths(@RequestBody Role role,@PathVariable List<Integer> aids) {
        String timestr = simpleDateFormat.format(new Date());
        role.setIsDeleted(0);
        role.setGmtCreate(timestr);
        role.setGmtModified(timestr);

        Integer nums = roleService.addRoleAndAuths(role,aids);

        if(nums>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed(ProjectConstant.USER_ADD_FAILED);
        }
    }



    @DeleteMapping(value = "/del/role/by/{rid}")
    @ApiOperation(value = "根据id删除单个角色")
    @CancelLoginIntercept
    public ResultEntity deleteRoleById(@PathVariable Integer rid) {

        Integer nums = roleService.delRoleById(rid);

        if (nums > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed(ProjectConstant.USER_DELETE_FAILED);
        }


    }


    @DeleteMapping(value = "/del/role/by/ids")
    @ApiOperation(value = "根据id批量删除角色")
    @CancelLoginIntercept
    public ResultEntity deleteRoleByIds(@RequestBody List<Integer> rids) {
        Integer nums = roleService.delRoleByIds(rids);

        if (nums > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed(ProjectConstant.USER_DELETE_FAILED);
        }

    }


    @PutMapping(value = "/update/role/by/id")
    @ApiOperation(value = "根据id修改角色名")
    @CancelLoginIntercept
    public ResultEntity updateRoleById(@RequestBody Role role) {
        String timestr = simpleDateFormat.format(new Date());

        role.setGmtModified(timestr);
        // 给密码加密
        // role.setRid(ProjectUtils.md5(role.getPassword()));

        Integer nums = roleService.updateRole(role);

        if (nums > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed(ProjectConstant.USER_CHANGE_FAILED);
        }
    }


    @PostMapping(value = "/get/roles/by/rname")
    @ApiOperation(value = "根据rname模糊分页查询")
    @CancelLoginIntercept
    public ResultEntity getRoleByName(@RequestBody RoleVO rolevo) {
        PageInfo pageinfo = roleService.findroleByrname(rolevo);

        Map<String,Object> rec = new HashMap<>();

        rec.put("total",pageinfo.getTotal());

        rec.put("data",pageinfo.getList());

        return ResultEntity.successWithData(rec);
    }


}
